package com.wondertek.poms.core.service.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.base.Strings;
import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.common.utils.CopyUtils;
import com.wondertek.poms.core.service.ISystemConfigService;
import com.wondertek.poms.dao.po.SystemConfig;
import com.wondertek.poms.dao.repo.ISystemConfigDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * <p><b>SystemConfigServiceImpl Description:</b> service层实现 </p>
 *
 * @author wzq
 * <p><b>DATE</b> 2019-06-27 13:48:26</p>
 */
@Service
@Scope("prototype")
public class SystemConfigServiceImpl extends ServiceImpl<SystemConfig, Long> implements ISystemConfigService {

    @Autowired
    private ISystemConfigDao systemConfigDao;

    @Override
    @Transactional
    public SystemConfig deleteLogic(Long id) {
        SystemConfig cp = jpaRepository.getOne(id);
        cp.setDeleted(Constants.DELETE_1);
        jpaRepository.save(cp);
        return cp;
    }

    @Override
    @CacheEvict(value = "poms-new:system-ckey", key = "#config.ckey")
    public SystemConfig save(SystemConfig config) {
        return jpaRepository.save(config);
    }

    @Override
    @CacheEvict(value = "poms-new:system-ckey", key = "#config.ckey")
    public void deleteById(SystemConfig config) {
        config.setDeleted(1);
        jpaRepository.save(config);
    }

    @Override
    public SystemConfig findByCkey(String ckey) {
        return systemConfigDao.findByCkey(ckey);
    }

    @Override
    public Page<SystemConfig> findAllData(SystemConfig systemConfig, int page, int rows, String sord, String sidx) {
        Pageable pageable = getPageable(page - 1, rows, sidx, sord);
        Specification<SystemConfig> specification = getWhereClause(systemConfig);
        return systemConfigDao.findAll(specification, pageable);
    }

    @Override
    @Transactional
    @CacheEvict(value = "poms-new:system-ckey", key = "#config.ckey")
    public SystemConfig merge(SystemConfig config) {
        SystemConfig dbo = jpaRepository.getOne(config.getId());
        CopyUtils.copyProperties(config, dbo);
        return jpaRepository.save(dbo);
    }

    /**
     * <p>Title: findAllBykey</p>
     * <p>Description: 根据ckey查询所有系统配置所有数据 </p>
     *
     * @param key
     * @return
     * @see
     */
    @Cacheable(value = "poms-new:system-ckey", key = "#key", unless = "#result==null")
    @Override
    public SystemConfig findAllBykey(String key) {
        SystemConfig systemConfig = new SystemConfig();
        systemConfig.setCkey(key);
        List<SystemConfig> all = findAll(Example.of(systemConfig));
        if (all != null && all.size() > 0) {
            systemConfig = all.get(0);
        } else {
            systemConfig = null;
        }
        return systemConfig;
    }

    /**
     * <p>Title: findAllBykey</p>
     * <p>Description: 根据ckey查询所有系统配置所有数据 </p>
     *
     * @param key
     * @return
     * @see
     */
    @CachePut(value = "poms-new:system-ckey", key = "#key", unless = "#result==null")
    @Override
    public SystemConfig cacheRefresh(String key, List<SystemConfig> sysConfigList) {
        for (SystemConfig systemConfig : sysConfigList) {
            if (systemConfig.getCkey().equals(key)) {
                return systemConfig;
            }
        }
        return null;
    }

    /**
     * <p><b>Title:</b> getWhereClause</p>
     * <p><b>Description:</b> where子句按条件查询</p>
     *
     * @param systemConfig
     * @return
     * @author huzhenjie
     */
    private Specification<SystemConfig> getWhereClause(SystemConfig systemConfig) {
        return new Specification<SystemConfig>() {
            @Override
            public Predicate toPredicate(Root<SystemConfig> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicate = new ArrayList<>();
                if (StrUtil.isNotBlank(systemConfig.getCkey())) {
                    predicate.add(cb.like(root.get("ckey"), "%" + systemConfig.getCkey() + "%"));
                }
                if (StrUtil.isNotBlank(systemConfig.getValue())) {
                    predicate.add(cb.like(root.get("value"), "%" + systemConfig.getValue() + "%"));
                }
                if (!Strings.isNullOrEmpty(systemConfig.getDescription())) {
                    predicate.add(cb.like(root.get("description"), "%" + systemConfig.getDescription() + "%"));
                }
                predicate.add(cb.equal(root.get("deleted"), "0"));
                Predicate[] pre = new Predicate[predicate.size()];
                return query.where(predicate.toArray(pre)).getRestriction();
            }
        };
    }
}